package com.api.shopapi.mapper;

import com.api.shopapi.entity.Building;
import com.api.shopapi.entity.Room;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface RoomMapper {
    @Insert("insert into room " +
            "(RoomCode,BuildingCode,BuildingName,Floor,RoomType,Number,Status,StaffCode,InstitutionCode,RoomTypeCode) " +
            "value(#{room.RoomCode},#{room.BuildingCode},#{room.BuildingName},#{room.Floor}," +
            "#{room.RoomType},#{room.Number}," +
            "#{room.Status},#{room.StaffCode},#{room.InstitutionCode},#{room.RoomTypeCode})")
    int insert(@Param("room") Room room);

    @Delete("delete from room where RoomCode=#{roomCode}")
    int deleteByRoomCode(@Param("roomCode") String roomCode);

    @Update("update room set BuildingCode = #{room.BuildingCode},BuildingName = #{room.BuildingName},Floor=#{room.Floor},RoomType=#{room.RoomType}," +
            "Number=#{room.Number},Status=#{room.Status},StaffCode=#{room.StaffCode},InstitutionCode=#{room.InstitutionCode},RoomTypeCode=#{room.RoomTypeCode} " +
            "where RoomCode=#{room.RoomCode}")
    int updateByRoomCode(@Param("room") Room room);

    @Select("select * from room")
    List<Room> getListAll();

    @Select("select * from room where Number=#{number} and BuildingCode=#{buildingCode} and InstitutionCode=#{institutionCode}  limit 1")
    Room getByName(@Param("number") String number, @Param("buildingCode") String buildingCode,@Param("institutionCode") String institutionCode);

    @Select("select * from room where InstitutionCode = #{institutionCode} and Status !=1 order by Floor,Number")
    List<Room> getListAllByInstitutionCode(@Param("institutionCode") String institutionCode);

    @Select("<script>" +
            "select * from room " +
            "where InstitutionCode = #{institutionCode}" +
            "<if test ='number !=-1'>" +
            "and Number like concat('%',#{number},'%') " +
            "</if>" +
            "<if test ='buildingCode !=\"00000000-0000-0000-0000-000000000000\" '> " +
            "and BuildingCode = #{buildingCode} " +
            "</if>" +
            "<if test ='roomTypeCode !=\"00000000-0000-0000-0000-000000000000\" '> " +
            "and RoomTypeCode = #{roomTypeCode} " +
            "</if>" +
            "<if test ='status !=-1 '> " +
            "and Status = #{status} " +
            "</if>" +
            "order by Floor,Number" +
            "</script>")
    List<Room> getPageList(@Param("institutionCode") String institutionCode,@Param("number") int number,@Param("buildingCode") String buildingCode, @Param("roomTypeCode") String roomTypeCode,@Param("status") int status);

    @Select("select * from room where RoomCode=#{roomCode} limit 1")
    Room getByRoomCode(@Param("roomCode") String roomCode);


    //查找楼宇信息
    @Select("SELECT * FROM building WHERE BuildingCode = (SELECT BuildingCode FROM room WHERE RoomCode = #{roomCode})")
    Building getByRoomCodeToBuilding(@Param("roomCode") String roomCode);


    @Select("select * from room where BuildingCode=#{buildingCode} order by Floor,Number")
    List<Room> getListByBuildingCode(@Param("buildingCode") String buildingCode);

    @Select("select * from room where BuildingCode=#{buildingCode} and Status!=1 order by Floor,Number")
    List<Room> getListByBuildingCodeAndStatus0(@Param("buildingCode") String buildingCode);

    @Select("select * from room r,room_type rt " +
            "where r.BuildingCode=#{buildingCode} " +
            "and rt.Num=#{num} " +
            "and r.Status!=1 " +
            "and rt.RoomTypeCode = r.RoomTypeCode " +
            "order by Floor,Number")
    List<Room> getListByBuildingCodeAndNumAndStatusNot1(@Param("buildingCode") String buildingCode,@Param("num") int num);

    @Select("select * from room where BuildingCode=#{buildingCode} and Status!=1 order by Floor,Number")
    List<Room> getListByBuildingCodeAndStatusNot1(@Param("buildingCode") String buildingCode);

    //统计没有禁用的房间
    @Select("select * from room where InstitutionCode = #{institutionCode} and Status != 1 order by Floor,Number")
    List<Room> getByInstitutionCodeAndStatusNot11(@Param("institutionCode") String institutionCode);

    @Select("SELECT * FROM room WHERE RoomTypeCode IN (SELECT RoomTypeCode FROM room_type WHERE PriceMonth = #{priceMonth} AND InstitutionCode = #{institutionCode} )")
    List<Room> getListInRoomTypePriceMonth(@Param("institutionCode") String institutionCode,@Param("priceMonth") double priceMonth);

    @Select("<script>" +
            "select * from room " +
            "where InstitutionCode in (select institutionCode from institution where governmentCode=#{governmentCode}) " +
            " order by Floor,Number" +
            "</script>")
    List<Room> listByGovCode(@Param("governmentCode") String governmentCode);


}
